package com.qf.fmw.dao.impl;

import com.qf.fmw.dao.UserDao;
import com.qf.fmw.entity.bo.MessageBo;
import com.qf.fmw.entity.bo.UserBo;
import com.qf.fmw.utils.DBManager;
import com.qf.fmw.utils.MD5Utils;

import java.util.List;

public class UserDaoImpl implements UserDao {

    // 根据用户名密码查询用户id
    @Override
    public Integer selectId(String name, String password) {
        return (Integer) DBManager.commonQuery("select u_id from user where u_status = 1 and u_name = ? and u_password = ?", name, password);
    }

    // 根据用户id查询用户角色id
    @Override
    public Integer selectRoleId(int adminId) {
        return (Integer) DBManager.commonQuery("select u_role from user where u_status=1 and u_id= ?", adminId);
    }

    // 查询所有用户
    @Override
    public List<UserBo> selectAllUsers() {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 1", UserBo.class);
    }

    // 注册
    @Override
    public boolean registerUser(String name, String password, String email, String sex) {
        int result = DBManager.commonUpdate("INSERT INTO user(u_name, u_password, u_email, u_sex) VALUES (?, ?, ?, ?)", name, password, email, sex);
        //判断插入操作是否成功，根据结果返回true或false
        return result > 0;
    }

    // 检查用户名是否存在
    @Override
    public boolean isUsernameExists(String name) {
        Long count = (Long) DBManager.commonQuery("select count(*) from user where u_name = ?", name);
        //判断查询结果是否大于0，大于0表示用户名已存在
        return count != null && count > 0;

    }
    // 根据性别查询用户
    @Override
    public List<UserBo> selectUserBySex(String sex) {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 1 AND u_sex = ?", UserBo.class, sex);
    }

    // 根据名字查询用户
    @Override
    public List<UserBo> selectUserByName(String name) {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 1 AND u_name like ?", UserBo.class, "%" + name + "%");
    }

    //展示用户个人信息
    @Override
    public List<MessageBo> selectUserMessage(int userId) {
        return DBManager.commonQuery("SELECT u_id,u_name,u_sex,u_email,m_registration_time FROM user u WHERE u_id = ?", MessageBo.class, userId);
    }

    //修改用户个人信息
    @Override
    public boolean updateUserMessage(int userId, String userName, String email, String sex) {
        int result = DBManager.commonUpdate("UPDATE user SET u_name = ?, u_email = ?, u_sex = ? WHERE u_id = ?", userName, email, sex, userId);
        return result > 0;
    }

    //修改用户密码
    @Override
    public boolean updateUserPassword(int userId, String newPassword) {
        String encryptedPassword = MD5Utils.md5(newPassword);
        int result = DBManager.commonUpdate("UPDATE user SET u_password = ? WHERE u_id = ?", encryptedPassword, userId);
        return result > 0;
    }


    // 根据名字和性别查询用户
    @Override
    public List<UserBo> selectUserByNameAndSex(String name, String sex) {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 1 AND u_name like ? AND u_sex = ?", UserBo.class, "%" + name + "%", sex);
    }

    // 删除用户
    @Override
    public Integer deleteUser(int userId) {
        return DBManager.commonUpdate("update user set u_status = 0 where u_id = ?", userId);
    }

    // 展示无效用户
    @Override
    public List<UserBo> selectInvalidUsers() {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 0", UserBo.class);
    }


    // 根据性别搜索无效用户
    @Override
    public List<UserBo> selectInvalidUserBySex(String sex) {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 0 and u_sex = ?", UserBo.class, sex);
    }

    // 根据姓名或者性别搜索无效用户（模糊查询）
    @Override
    public List<UserBo> searchInvalidUsers(String query) {
        return DBManager.commonQuery("select u_id, u_email, u_name, u_sex, u_role from user where u_status = 0 and (u_name like ? or u_email like ?)", UserBo.class, "%" + query + "%", "%" + query + "%");
    }

    // 删除无效用户
    @Override
    public Integer deleteInvalidUser(int userId) {
        return DBManager.commonUpdate("delete from user where u_id = ? and u_status = 0", userId);
    }

}




